package magicboard.model
{
	import flash.geom.Point;
	
	import mx.utils.StringUtil;
	
	
	/**
	 * Model with figures
	 */
	public class Model
	{
		
		// --------------------------------------------------------------------------------------------
		public function Model()
		{
		}

		// --------------------------------------------------------------------------------------------
		public function get objects():Array { return _objects; }
		
		// --------------------------------------------------------------------------------------------
		public function addObject(obj:GeometryObject):void{
		  _objects.push(obj);
		}
		
		// --------------------------------------------------------------------------------------------
		public function clear():void{
		  
		  while(_objects.length > 0)
		  	_objects.pop();
		}
		
		// --------------------------------------------------------------------------------------------
		public function loadObjFromString(data:String):void{
			var map:Array = data.split(":");
			
			if(map.length == 2){
				var name:String = StringUtil.trim(map[0]);
				var points:Array = map[1].match(/[\d\.]+/g);
				var obj:GeometryObject = null;
			
				if(name == "polyline"  && points.length > 5){
					obj = new Polyline();
	      	for(var i:int = 1; i < points.length; i += 2){
	      		(obj as Polyline).addPoint(new Point(points[i-1], points[i]));
	      	}
	      	
	   		}
	   		else if(name == "line" && points.length == 4){
	   			obj = new LineSegment(
	   				new Point(points[0], points[1]), new Point(points[2], points[3]));
	   		}
	   		
	   		if(obj != null)
	   			addObject(obj);
   		}
		}
		
		
		// --------------------------------------------------------------------------------------------
		public function removeLastObj():void{
		  
		  if(_objects.length > 0)
		  	_objects.pop();
		}
		
		// --------------------------------------------------------------------------------------------
		// Private members
		private var _objects:Array = new Array();;
	}
}